﻿using System;
using System.Threading;

namespace AppDomainTestfr
{
    internal class Program
    {
        static void Main(string[] args)
        {
        }

        private static void MarShalling()
        {
            AppDomain app1 = Thread.GetDomain();
            string app1name = app1.FriendlyName;
            Console.WriteLine($"app1名称 {app1name}");

            AppDomain app2 = null;
            //按引用封送
            Console.WriteLine($"demo1{Environment.NewLine}");

            app2 = AppDomain.CreateDomain("APP", null, null);
            MarshalByRefType mbrt = null;
        }
    }

    /// <summary>
    /// 跨越 app domain 边界 按引用封送
    /// </summary>
    public sealed class MarshalByRefType : MarshalByRefObject
    {
        public MarshalByRefType()
        {
            Console.WriteLine(this.GetType().ToString() + "----" + Thread.GetDomain().FriendlyName);
        }

        public void SomeMethod()
        {
            Console.WriteLine(Thread.GetDomain().FriendlyName);
        }

        public MarshalByValType MethodReturn()
        {
            Console.WriteLine(Thread.GetDomain().FriendlyName);

            MarshalByValType marshalByValType = new MarshalByValType();
            return marshalByValType;
        }
    }

    /// <summary>
    /// 跨边界 按值封送
    /// </summary>
    /// 
    [Serializable]
    public sealed class MarshalByValType : Object
    {
        private DateTime dateTime = DateTime.Now;

        public MarshalByValType()
        {
            Console.WriteLine($"{this.GetType().ToString()},{Thread.GetDomain().FriendlyName},{dateTime}");
        }

        public override string ToString()
        {
            return dateTime.ToLongDateString();
        }

    }
  
    /// <summary>
    /// 不能跨 app domain 边界封送
    /// </summary>
    public sealed class NonMarshalableType : Object
    {
        public NonMarshalableType()
        {
            Console.WriteLine(Thread.GetDomain().FriendlyName);
        }
    }
}
